package cn.shujuhai.cms.service.thymeleaf;

import cn.shujuhai.cms.config.TemplateEngineConfig;
import cn.shujuhai.cms.domain.SysChannel;
import cn.shujuhai.cms.service.SysChannelService;
import cn.shujuhai.cms.service.SysTemplateService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

/**
 * @Author: zhangDeZhong
 * @Date: 2024/12/5 12:01
 * @Description: 提供栏目模板访问的Service实现
 **/
@Service
@RequiredArgsConstructor
public class ColumnService {
    private final SysChannelService sysChannelService;
    private final SysTemplateService sysTemplateService;
    private final TemplateEngineConfig templateEngine;
    private final ChannelTool channelTool;
    private final InfoTool infoTool;
    private final SysTool sysTool;
    private final StaticHtmlService staticHtmlService;

    @Value("${server.servlet.context-path}")
    private String base;

    /**
     * 根据栏目的url动态选择并传递数据渲染相应的模板
     *
     * @param url      栏目路径
     * @param key      搜索关键字
     * @param pageNum  页码
     * @param pageSize 每页大小
     * @return 渲染后的模板内容
     */
    public String getColumn(String url, String key, Integer pageNum, Integer pageSize) {
        SysChannel sysChannel = this.sysChannelService.getOne(Wrappers.<SysChannel>lambdaQuery().eq(SysChannel::getUrlKey, url));
        if (sysChannel == null) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "页面不存在");
        }
        return renderTemplate(sysChannel, pageNum, pageSize, key);
    }

    /**
     * 渲染并将数据动态加载到模板
     *
     * @param sysChannel 栏目信息
     * @param pageNum    页码
     * @param pageSize   每页大小
     * @param key        搜索关键字
     * @return 渲染后的模板内容
     */
    private String renderTemplate(SysChannel sysChannel, Integer pageNum, Integer pageSize, String key) {
        String templateUrl = this.sysTemplateService.getById(sysChannel.getTemplateId()).getTemplateUrl();
        TemplateEngine dynamicTemplateEngine = templateEngine.getTemplateEngine();
        Context context = new Context();
        context.setVariable("key", key);
        context.setVariable("channel", sysChannel);
        context.setVariable("pageNum", pageNum);
        context.setVariable("pageSize", pageSize);
        context.setVariable("base", base);
        context.setVariable("channelTool", channelTool);
        context.setVariable("infoTool", infoTool);
        context.setVariable("sysTool", sysTool);
        context.setVariable("language", "zh-CN");
        context.setVariable("check", "check");
        context.setVariable("state", 0);
        this.staticHtmlService.setContextHeaderLink("zh-CN", context);
        // 使用动态模板引擎渲染模板
        return dynamicTemplateEngine.process(templateUrl, context);
    }


}
